package com.dhcc.controller;

import com.dhcc.api.R;
import com.dhcc.entity.ClientUser;
import com.dhcc.jwt.JwtTokenUtil;
import com.dhcc.service.AuthService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.controller
 * @Title AuthController
 * @Date 2024/10/11 下午3:46
 * @Author LiuGuoting
 */
@RestController
@RequestMapping("/auth/client")
public class ClientAuthController {

    @Resource
    private AuthService authService;
    @Resource
    @Qualifier("clientAuthManager")
    private AuthenticationManager clientAuthenticationManager;

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public R login(@RequestBody ClientUser clientUser) {
        try {
            Map<String, Object> data = authService.login(clientUser.getUserName(), clientUser.getUserPassword(), clientAuthenticationManager,"client");
            if (data == null) {
                return R.fail("登录失败，请重试！");
            }
            return R.data(data, "登录成功");
        } catch (DisabledException e) {
            // 用户已被禁用
            return R.fail("该用户已被禁用，请联系管理员！");
        } catch (LockedException e) {
            // 用户账户被锁定
            return R.fail("该用户账户已被锁定，请联系管理员！");
        } catch (BadCredentialsException e) {
            // 用户名或密码错误
            return R.fail("用户名或密码错误，请重新登录！");
        } catch (AuthenticationException e) {
            // 其他认证异常
            return R.fail("登录失败，请重试！");
        }
    }

    // /**
    //  * 注册接口
    //  */
    // @PostMapping("/register")
    // public R register(String username, String password) {
    //     SysUser userByUsername = sysUserService.getUserByUsername(username);
    //     if (userByUsername != null) {
    //         return R.fail("用户名已存在,请更换用户名！");
    //     }
    //     SysUser sysUser = new SysUser();
    //     sysUser.setUserName(username);
    //     sysUser.setUserPassword(passwordEncoderUtil.encode(password));
    //     boolean row = sysUserService.registerUser(sysUser);
    //     if (!row) {
    //         return R.fail("注册失败");
    //     }
    //     return R.success("注册成功");
    // }

    /**
     * 刷新token接口
     */
    @PostMapping("/refreshToken")
    public R refreshToken(HttpServletRequest request) {
        // 从请求头中获取 Authorization 信息
        String token = request.getHeader("dhcc-auth");
        token = JwtTokenUtil.getToken(token);
        if (token == null || token.isEmpty()) {
            return R.fail("Token 不存在或为空");
        }
        Map<String, Object> data = authService.refreshToken();
        return R.data(data, "刷新令牌成功！");
    }

    /**
     * 注销用户，删除 Redis 中的缓存
     * @param request 请求对象
     * @return R 返回结果
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        // 从请求头中获取 Authorization 信息
        String token = request.getHeader("dhcc-auth");
        token = JwtTokenUtil.getToken(token);
        if (token == null || token.isEmpty()) {
            return R.fail("Token 不存在或为空");
        }
        boolean isDeletedAuth = authService.logout();
        if (isDeletedAuth) {
            return R.success("注销成功");
        } else {
            return R.fail("已注销，请重新登录");
        }
    }



    /**
     * 验证码接口
     */
    @GetMapping("/captcha")
    public String captcha() {
        return "暂未开放验证码功能！";
    }
}
